package com.contory.ssn_lgnd.framework.gl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import android.opengl.GLES20;

public class Texture_NoBitmapBuffered implements TexturePtr{
	int textureId;
	int minFilter;
	int magFilter;
	public int width;
	public int height;
	public ByteBuffer buffer;
	
	public Texture_NoBitmapBuffered(int width, int height) {
		this.width = width;
		this.height = height;
		//load();
	}
	
	@Override
	public void load() {
		int[] textureIds = new int[1];
		GLES20.glGenTextures(1, textureIds, 0);
		textureId = textureIds[0];
		textureIds= null;
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
		
		//buffer = ByteBuffer.allocate(width * height * 4);
		//buffer.order(ByteOrder.nativeOrder());
		//GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, 
		//		height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
		setFilters(GLES20.GL_NEAREST, GLES20.GL_NEAREST);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
	}
	
	private void setFilters(int minFilter, int magFilter) {
		this.minFilter = minFilter;
		this.magFilter = magFilter;
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, minFilter);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, magFilter);
	}
	
	@Override
	public int getTexId() {
		return textureId;
	}

	@Override
	public void bind(ESShader shader, String texMapName, int activeTexPos) {
		int activeTexPosParam = activeTexPos + GLES20.GL_TEXTURE0;
		GLES20.glActiveTexture(activeTexPosParam);
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
		GLES20.glUniform1i(shader.getUniform(texMapName), activeTexPos);
	}
	
	@Override
	public void dispose() {
		GLES20.glBindTexture ( GLES20.GL_TEXTURE_2D, textureId );
		int[] textureIds = { textureId };
		GLES20.glDeleteTextures(1, textureIds, 0);
	}
}
